package com.ccsoft.leetcode.service;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 28. 实现 C++ 中的 strStr()
 * 即查找字符串B在字符串A中第一次出现的位序
 */
@Service
public class StrStr {
    public int strStr(String haystack, String needle) {
        if( needle == null || needle.length()<=0 ) return 0;
        if( haystack == null || haystack.length()<=0 ) return -1;

        int n = haystack.length(), m = needle.length();
        for(int i=0;i + m <= n;i++){
            boolean flag = true;
            for(int j=0;j<m;j++){
                if(haystack.charAt(i+j) != needle.charAt(j)) {
                    flag = false;
                    break;
                }
            }
            if( flag )
                return i;
        }
        return -1;
    }
}
